package cn.dwong.msgGateway.base;

/**
 * @author 73202
 * @date 2022/3/9 15:32
 */
public class ApiResponse {
    /**
     * 响应码
     */
    private int code;
    /**
     * 提示信息
     */
    private String message;

    public ApiResponse(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public ApiResponse() {
        this.code = Status.SUCCESS.getCode();
        this.message = Status.SUCCESS.getStandardMessage();
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }


    public static ApiResponse ofMessage(int code, String message) {
        return new ApiResponse(code, message);
    }

    public static ApiResponse ofSuccess() {
        return new ApiResponse(Status.SUCCESS.getCode(), Status.SUCCESS.getStandardMessage());
    }

    public static ApiResponse ofBadRequest(){
        return new ApiResponse(Status.BAD_REQUEST.getCode(), Status.BAD_REQUEST.getStandardMessage());
    }

    public static ApiResponse ofInterNalServerError(){
        return new ApiResponse(Status.INTERNAL_SERVER_ERROR.getCode(), Status.INTERNAL_SERVER_ERROR.getStandardMessage());
    }

    public static ApiResponse ofUnauthorized(){
        return new ApiResponse(Status.UNAUTHORIZED.getCode(), Status.UNAUTHORIZED.getStandardMessage());
    }

    public static ApiResponse ofCurrentlimiting(){
        return new ApiResponse(Status.CURRENT_LIMITING.getCode(), Status.CURRENT_LIMITING.getStandardMessage());
    }
    public static ApiResponse ofCutoff(){
        return new ApiResponse(Status.CUT_OFF.getCode(), Status.CUT_OFF.getStandardMessage());
    }

    public enum Status {
        SUCCESS(200, "success"),
        BAD_REQUEST(400, "请求参数错误"),
        UNAUTHORIZED(403,"禁止未经授权访问"),
        CURRENT_LIMITING(409,"客户端调用频率过高，当前处于限流中"),
        CUT_OFF(429,"达到调用频率"),
        INTERNAL_SERVER_ERROR(500, "内部服务器错误");

        private int code;
        private String standardMessage;

        Status(int code, String standardMessage) {
            this.code = code;
            this.standardMessage = standardMessage;
        }

        public int getCode() {
            return code;
        }

        public void setCode(int code) {
            this.code = code;
        }

        public String getStandardMessage() {
            return standardMessage;
        }

        public void setStandardMessage(String standardMessage) {
            this.standardMessage = standardMessage;
        }
    }
}
